package com.wy.OneClient;

import com.wy.OneClient.pojo.Data;
import com.wy.OneClient.pojo.DataType;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/03/03/21:12
 * @target 心若有所向往，何惧道阻且长
 */
public class Info {
    public static final Scanner SCANNER = new Scanner(System.in);
    //    TODO:  考虑创建一个可以一直使用的对象。防止oom  防止发送信息一直创建对象
    public static Data data = new Data<String>(null, DataType.COMMAND);

    public static String LOCALADDRESSDEFAULTIP;
    public static Integer LOCALADDRESSDEFAULTPORT;
    public static String TARGETADDRESSIP;
    public static Integer TARGETADDRESSPORT;
    public static String USER_NAME;
    public static String USER_PHONE;
    //    当前连接的标示 如果当前两个线程通着的，那么就一直互相发送这个标示。表示通路。这样的话算是一个bug把， 好像也不算。
    public static final String CONNECTING = "wish for world peace";
    //    间隔时间。
    public static Integer Interval_Time;

    //    默认发送文件的文件夹
    public static String DEFAULT_SEND_FILE;
    //    默认接受文件的文件夹
    public static String DEFAULT_ACCEPT_FILE;


    //        初始化代码，这一步是读取配置文件的。
    static {
        try {
            Properties properties = new Properties();

            properties.load(new InputStreamReader(Objects.requireNonNull(Info.class.getClassLoader().getResourceAsStream("application.properties")), StandardCharsets.UTF_8));
            String local_address_default_ip = properties.getProperty("LOCAL_ADDRESS_DEFAULT_IP");

            String localIp = InetAddress.getLocalHost().toString();
            LOCALADDRESSDEFAULTIP = localIp.substring(localIp.lastIndexOf("/") + 1);

            LOCALADDRESSDEFAULTPORT = Integer.parseInt(properties.getProperty("LOCAL_ADDRESS_DEFAULT_PORT"));

            TARGETADDRESSIP = properties.getProperty("TARGET_ADDRESS_IP");

            TARGETADDRESSPORT = Integer.parseInt(properties.getProperty("TARGET_ADDRESS_PORT"));

            USER_NAME = properties.getProperty("USER_NAME");

            USER_PHONE = properties.getProperty("USER_PHONE");

            Interval_Time = Integer.parseInt(properties.getProperty("Interval_Time"));

            DEFAULT_SEND_FILE = properties.getProperty("DEFAULT_SEND_FILE");

            DEFAULT_ACCEPT_FILE = properties.getProperty("DEFAULT_ACCEPT_FILE");
        } catch (UnknownHostException e) {
            System.out.println("可能是无法获取本机的ip");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("文件没有发现异常");
        } catch (IOException e) {
            System.out.println("IO流异常");
        }
    }
}